package com.tarena.lbs.business.servcie.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.constant.BusinessTypes;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.StatisticsVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.BusinessService;
import com.tarena.lbs.common.enums.Roles;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @DubboReference
    private AttachApi attachApi;

    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private StoreRepository storeRepository;
    @Override
    public void save(BusinessParam param) {
        log.info("新增商家参数:{}",param);
        checkRole(Roles.ADMIN);
        BusinessPO businessPO = new BusinessPO();
        businessPO.setEntryTime(new Date());
        BeanUtils.copyProperties(param, businessPO);
        Integer id =businessRepository.save(businessPO);
//        bindPictures(businessPO.getId(),param);
    }

    @Override
    public PageResult<BusinessVO> businessInfoList(BusinessQuery query) {
        PageResult<BusinessVO> pageResult = businessRepository.businessInfoList(query);
        return pageResult;
    }

    @Override
    public BusinessStoreVO businessStoreDetail(Integer businessId) {
        BusinessStoreVO businessStoreVO = businessRepository.businessStoreDetail(businessId);
        return businessStoreVO;
    }

    @Override
    public void updateBusiness(BusinessParam param) {
        BusinessPO businessPO = new BusinessPO();
        BeanUtils.copyProperties(param,businessPO);
        businessPO.setEntryTime(new Date());
        businessRepository.updateBusiness(businessPO);
    }


    private void checkRole(Roles roles) {
        if (!roles.equals(Roles.ADMIN)) {
            throw new IllegalArgumentException("只有平台管理员才能新增商家");
        }
    }


    @Override
    public Result<Map<String, Integer>> statistics() {
        List<StatisticsVO> business =businessRepository.selectAllBusiness();
        Map<String, Integer> statistics = new HashMap<>(business.size());
        for (StatisticsVO merchant : business) {
            int shopCount = businessRepository.countStoreById(merchant.getId());
            statistics.put(merchant.getBusinessName(), shopCount);
        }
        log.info("商家统计结果{}",statistics);
        return new Result<>(statistics);
    }
    //    public Result<BusinessStoreVO> getBusiDetail(Integer businessId) {
//        BusinessStoreVO businessStoreVO = businessRepository.getBusiDetail(businessId);
//        List<StoreVO> storeVOList = storeRepository.selectStoreListByBusinessId(businessId);
//        businessStoreVO.setStoreVOList(storeVOList);
//        log.info("查询商家详情：{}",businessStoreVO);
//        return new Result<>(businessStoreVO);
//    }


    @Override
    public BusinessStoreVO getBusiDetail(Integer articleId) {
        BusinessStoreVO businessStoreVO = businessRepository.getBusinessDetail(articleId);
        log.info("查询商家详情：{}",businessStoreVO);
        return businessStoreVO;
    }
}
